<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :inline="true">
      <el-form-item label="支付名称" prop="paymentName">
        <el-input v-model="form.paymentName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary"  v-hasPermi="['ims:payType:add']" plain icon="el-icon-plus" @click="updateRow(0,{})">新增</el-button>
    <el-table
      v-loading="loading"
      :data="list"
    >
      <el-table-column type="index" label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="paymentName" label="支付名称" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateRow(1,scope.row)">{{ scope.row.paymentName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="paymentCode" label="支付编码" align="center"/>
      <el-table-column prop="original" label="期初金额(元)" align="center"/>
      <el-table-column prop="income" label="收入金额(元)" align="center"/>
      <el-table-column prop="outcome" label="支出金额(元)" align="center"/>
      <el-table-column prop="balance" label="余额(元)" align="center"/>
      <el-table-column prop="enable" label="是否启用" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enable" :active-value="0"
                     :inactive-value="1"
                     @change="changeStatus(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center"/>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            v-hasPermi="['ims:payType:update']"
            @click="updateRow(2,scope.row)"
          >修改
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['ims:payType:delete']"
            @click="updateRow(3,scope.row)"
          >删除
          </el-button>
<!--          <el-button-->
<!--            type="text"-->
<!--            v-hasPermi="['ims:payType:detail']"-->
<!--            @click="updateRow(4,scope.row)"-->
<!--          >收支明细-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="form.pageNum"
      :limit.sync="form.pageSize"
      @pagination="getList"
    />
    <updateDialog ref="updateDialog" @ok="handleQuery"></updateDialog>
    <detailDialog ref="detailDialog" @ok="handleQuery"></detailDialog>
  </div>
</template>

<script>
import updateDialog from "./updateDialog";
import detailDialog from "./detailDialog";
import {changePayType, deletePayType, listPayType} from "../../../../api/payMng";

export default {
  name: 'PayType',
  components: {updateDialog, detailDialog},
  data() {
    return {
      loading: false,
      list: [],
      form: {
        orderNo: "",
        status: "",
        pageNum: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.handleQuery()
    },
    handleQuery() {
      this.form.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('form')
      this.handleQuery()
    },
    changeStatus(row) {
      let req = {id: row.id, enable: row.enable}
      changePayType(req).then(res => {
        this.$message.success('操作成功')
      })
    },
    getList() {
      this.loading = false
      listPayType(this.form).then(res => {
        this.list = res.rows
        this.total = res.total
      }).finally(e => {
        this.loading = false
      })
    },
    updateRow(flag, row) {
      if (flag == 0 || flag == 1 || flag == 2) {
        this.$refs.updateDialog.show(flag, row)
      }
      if (flag == 3) {
        this.$modal.confirm('是否确定删除').then(res => {
          deletePayType({id: row.id}).then(res => {
            this.handleQuery()
            this.$message.success('删除成功')
          })
        })
      }
      if (flag == 4) {
        this.$refs.detailDialog.show(row)
      }
    }
  }
}
</script>
